﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using LeanBoard.Models;

namespace LeanBoard.HtmlBuilders
{
    public class ItemHtmlBuilder
    {
        private const string _profileImagesPath = "/Client/Profiles/";

        private ProfilesConfigSection _profilesConfigSection;
        private ProfilesElementCollection Profiles
        {
            get
            {
                if (_profilesConfigSection == null)
                {
                    _profilesConfigSection = (ProfilesConfigSection)ConfigurationManager.GetSection("profilesSection");
                }
                if (_profilesConfigSection == null)
                {
                    return new ProfilesElementCollection();
                }
                return _profilesConfigSection.Profiles;
            }
        }

        public string BuildBoardItemsHtml(IList<Item> items)
        {
            string html = string.Empty;

            foreach (Item item in items)
            {
                string headerClass = item.ItemType.ToLower() + "Header";
                string progressClass = item.ItemType.ToLower() + "Progress";
                string bodyClass = item.ItemType.ToLower() + "Body";

                html += String.Format("<li class='{0}' id='{1}'>" + Environment.NewLine, "item " + bodyClass, item.Id);
                html += String.Format("<div class='{0}'>" + Environment.NewLine, "handle " + headerClass);
                html += String.Format("<div class='{0}' style='width:{1}%;'></div>" + Environment.NewLine, "progress " + progressClass, item.Progress);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemPriority", "!" + item.Priority);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemNumber", item.Id);
                html += "</div>" + Environment.NewLine;
                html += String.Format("<div class='{0}'>", "content");
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "crResponsible " + bodyClass, item.DevResponsible);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "crTitle " + bodyClass, item.Title);
                html += String.Format("<div class='{0}'>{1}</div>" + Environment.NewLine, "itemUnid", item.Unid);
                html += "</div>" + Environment.NewLine;
                html += "</li>" + Environment.NewLine;
                html += Environment.NewLine;
            }

            return html;
        }

        public string BuildStandupItems(ItemsCollection items)
        {
            List<Item> forStandup = new List<Item>();
            forStandup.AddRange(items.Development);
            forStandup.AddRange(items.ReadyForCodeReview);
            forStandup.AddRange(items.CodeReview);
            forStandup.AddRange(items.ReadyForApporval);
            forStandup.AddRange(items.Testing);

            return BuildOverlayStandupItemsHtml(forStandup);
        }

        public string BuildAllItems(ItemsCollection items)
        {
            List<Item> forStandup = new List<Item>();
            forStandup.AddRange(items.Scheduled);
            forStandup.AddRange(items.Analysis);
            forStandup.AddRange(items.AnalysisReadyForReview);
            forStandup.AddRange(items.AnalysisReview);
            forStandup.AddRange(items.AnalysisDone);
            forStandup.AddRange(items.ReadyForDevelopment);
            forStandup.AddRange(items.Development);
            forStandup.AddRange(items.ReadyForCodeReview);
            forStandup.AddRange(items.CodeReview);
            forStandup.AddRange(items.ReadyForApporval);
            forStandup.AddRange(items.Testing);
            forStandup.AddRange(items.Done);

            return BuildOverlayAllItemsHtml(forStandup);
        }

        public string BuildResponsiblesGallery(string physicalApplicationPath)
        {
            string html = string.Empty;

            html += "<div>" + Environment.NewLine;
            foreach (ProfilesElement profile in Profiles)
            {
                html += GetGalleryImageHtml(profile);
            }
            html += "</div>" + Environment.NewLine;

            return html;
        }

        private static string GetGalleryImageHtml(ProfilesElement profile)
        {
            string html = string.Empty;
            html += String.Format("<div class='{0}'>" + Environment.NewLine, "profileImage");
            html += String.Format("<img src='{0}' alt='' width='178' height='178' />" + Environment.NewLine, Path.Combine("../../", _profileImagesPath, profile.ImageName));
            html += String.Format("<h2>{0}</h2>" + Environment.NewLine, profile.DisplayName);
            html += "</div>" + Environment.NewLine;

            return html;
        }

        private string BuildOverlayStandupItemsHtml(IEnumerable<Item> forStandup)
        {
            string html = string.Empty;

            foreach (Item item in forStandup)
            {
                html += GetSingleItemHtml(item, "standup");
            }

            return html;
        }

        private string BuildOverlayAllItemsHtml(IEnumerable<Item> forAllItems)
        {
            string html = string.Empty;

            foreach (Item item in forAllItems)
            {
                html += String.Format("<div class='{0}' id='{1}'>" + Environment.NewLine, "apple_overlay " + item.ItemType.ToLower() + "Body", "singleOverlay" + item.Id);
                html += String.Format("<div class='{0}'>Overview</div>", "overview");
                html += String.Format("<div class='{0}'>Details</div>", "details");
                html += String.Format("<div class='{0}'>Close</div>", "close");
                html += GetSingleItemHtml(item, "all");
                html += String.Format("<div class='{0}'></div>", "externalContentFrame");
                html += "</div>" + Environment.NewLine;
            }

            return html;
        }

        private string GetSingleItemHtml(Item item, string idPrefix)
        {
            string html = string.Empty;
            html += String.Format("<div class='{0}' id='{1}'>", "itemPopup", idPrefix + item.Id);

            html += String.Format("<div class='{0}'>", "itemInfo");
            html += String.Format("<div>CR {0} - {1}</div>", item.Id, item.Status);
            html += String.Format("<div>Priority: {0}</div>", item.Priority);
            html += String.Format("<div>Done: {0}%</div>", item.Progress);
            html += "</div>";

            html += "<div style='float: right;' class='profileImage'>";
            html += String.Format("<img src='{0}' alt='' width='178' height='178'>", "../../Client/Profiles/" + (Profiles[item.DevResponsible] == null ? "Anonymous.jpg" : Profiles[item.DevResponsible].ImageName));
            html += String.Format("<h2>{0}</h2>", item.DevResponsible);
            html += "</div>";
            html += String.Format("<div class='{0}'>{1}</div>", "itemTitle", item.Title);
            html += String.Format("<div class='{0}'>{1}</div>", "itemUnid", item.Unid);

            html += "</div>";
            html += Environment.NewLine;

            return html;
        }
    }
}